/**
 * 
 */
package Utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author qiyuan.zqy
 *
 */
public class TimeUtils {
	
	public static final String dateStrFormat = "yyyyMMdd";
	public static final long oneDaySec = 86400; 
	
	
	 public static String getNowDate() {
		  Date currentTime = new Date();
//		  System.out.println(currentTime);
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  String dateStr = formatter.format(currentTime);
		  return dateStr;
		 }
	 
	 //format of dateStr is yyyyMMdd, beforeDays means days before date str, need >0
	 public static long getDayTimeInt(String dateStr, int beforeDays){
		 int year = Integer.parseInt(dateStr.substring(0, 4));
		 int month = Integer.parseInt(dateStr.substring(4, 6));
		 int day = Integer.parseInt(dateStr.substring(6, 8));
		 
		 Calendar dayCalendar = Calendar.getInstance();
		 dayCalendar.set(Calendar.YEAR, year);
		 dayCalendar.set(Calendar.MONTH, month-1);
		 dayCalendar.set(Calendar.DAY_OF_MONTH, day);
		 dayCalendar.set(Calendar.HOUR_OF_DAY, 0); 
		 dayCalendar.set(Calendar.SECOND, 0); 
		 dayCalendar.set(Calendar.MINUTE, 0); 
		 dayCalendar.set(Calendar.MILLISECOND, 0); 
		 
		 System.out.println(dayCalendar.getTime().toString());
		 
		 long daySec = dayCalendar.getTimeInMillis()/1000;
		 
		 return (daySec - beforeDays*oneDaySec);
	 }
	 
	//format of dateStr and return value is yyyyMMdd, beforeDays means days before date str, need >0
	 public static String getDayTime(String dateStr, int beforeDays){
		 int year = Integer.parseInt(dateStr.substring(0, 4));
		 int month = Integer.parseInt(dateStr.substring(4, 6));
		 int day = Integer.parseInt(dateStr.substring(6, 8));
		 
		 Calendar dayCalendar = Calendar.getInstance();
		 dayCalendar.set(Calendar.YEAR, year);
		 dayCalendar.set(Calendar.MONTH, month-1);
		 dayCalendar.set(Calendar.DAY_OF_MONTH, day);
		 dayCalendar.set(Calendar.HOUR_OF_DAY, 0); 
		 dayCalendar.set(Calendar.SECOND, 0); 
		 dayCalendar.set(Calendar.MINUTE, 0); 
		 dayCalendar.set(Calendar.MILLISECOND, 0); 
		 
		dayCalendar.add(Calendar.DAY_OF_MONTH, -beforeDays);
		Date date = dayCalendar.getTime();
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String resultStr = formatter.format(date);
		return resultStr;
	 }
	 
	 //format of input str is yyyy-MM-dd HH:mm:ss
	 public static Date getDateFromStr(String str){
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		 Date date = null;
		 try{
			 date = sdf.parse(str);
		 }catch(Exception ex){
			 //has no handle...ai...
		 }
		 
		 return date;
	 }
	 
	 public static void main(String[] args){
//		 Calendar today = Calendar.getInstance();
//		 today.set(Calendar.HOUR_OF_DAY, 0); 
//		 today.set(Calendar.SECOND, 0); 
//		 today.set(Calendar.MINUTE, 0); 
//		 today.set(Calendar.MILLISECOND, 0); 
//		 
//		 long todaySec = today.getTimeInMillis()/1000;
//		 
//		 System.out.println("today is " + todaySec);
//		 
//		 for(int i=0; i<3; i++){
//			
//			today.add(today.DATE, -1);
//			long nowSec =  today.getTimeInMillis()/1000;
//			System.out.println("before date i is "+ nowSec);
//			long chaJu = todaySec - nowSec;
//			System.out.println("cha ju is " + chaJu);
//			todaySec = nowSec;
//			System.out.println(today.getTime().toString());
//			
//		 }
//		 System.out.println("My 0 is " + getDayTimeInt("20140119",0));
//		 System.out.println("My 1 is " + getDayTimeInt("20140119",1));
//		 System.out.println("My 2 is " + getDayTimeInt("20140119",2));
		 
//		 String dateStr = "20140119";
//		 System.out.println(dateStr.substring(0, 4));
//		 System.out.println(dateStr.substring(4, 6));
//		 System.out.println(dateStr.substring(6, 8));
		 
		 System.out.println(getDayTimeInt("20131101", 0));
	 }
}
